/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telemetria.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.telemetria.HibernateUtil;
import com.telemetria.entity.endereco.Endereco;

/**
 *
 * @author ARGLAS
 */
public class EnderecoDaoImpl implements EnderecoDao{

    @Override
    public void saveEndereco(Endereco endereco) {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.save(endereco);
        t.commit(); 
        session.close();
        
    }
    
    public void deleteEndereco(Endereco endereco) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(endereco);
        t.commit();
        session.close();
    }
    
    public Endereco findEnderecoByPK(long pk) {    	
    	Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        Query query = session.createQuery("from Endereco e where e.id = :pk");
        query.setParameter("pk", pk);
        Endereco endereco = (Endereco) query.uniqueResult();
        t.commit();        
        session.close();
        return endereco;
    }
    
}
